<?php 
class TmenuController extends AppController {

	var $name = 'Tmenu';
	var $components = array('Xacl');

	/**
     * load the menu  tree
     *
     */
	function index() {

		$this->layout = 'tuser';
		$menu_tree = '<table border="" cellpadding="2" cellspacing="2" width="100%">
                    <tr><td width="20%" colspan="2">主菜单</td>
						<td><a href="/tmenu/newsub/0" target="menuframe_edit">增加主菜单</a></td>
                    </tr>';

		$treeData = $this->Tmenu->getSubMenu('0');

		foreach ($treeData as $val) {
			if (! empty($val['f_name'])) {
				$menu_tree .= '<tr>
                        	<td width="10%"></td>
                        	<td colspan="2"><a href="/tmenu/edit/'.$val['id'].'" target="menuframe_edit">'.$val['f_name'].'</a></td>
                        </tr>';
				if (count($val['sub']) > 0) {

					foreach ($val['sub'] as $vl) {
						if (! empty($vl['f_name'])) {
							$menu_tree .= '<tr>
                        	<td width="20%" colspan="2"></td>
                        	<td><a href="/tmenu/edit/'.$vl['id'].'" target="menuframe_edit">'.$vl['f_name'].'</a></td>
                        </tr>';
						}
					}
				}
			}
		}
		$menu_tree .= '</table>';
		$this->set('menu_tree', $menu_tree);

	}
	/**
     * edit the menu info
     *
     * @param int $id menu id
     */
	function edit($id) {
		$menu_info = $this->Tmenu->find("id='$id'");

		$this->set('menu_info', $menu_info['Tmenu']);
		$this->layout = NULL;
	}
	/**
     * insert new sub menu form
     *
     * @param int $id menu id
     */
	function newsub($id = 0) {
		$this->set('pid', $id);
		$this->layout = NULL;
	}
	/**
     * delete menu
     *
     * @param int $id menu id
     */
	function del($id) {
		$this->Tmenu->id = $id;
		$this->Tmenu->del();
		$this->flash('删除成功', '/tmenu/newsub/0');
		return;
	}
	/**
     * insert new sub menu action
     *
     */
	function dosub() {
		$data = Array('f_pid'=>isset($_POST['pid']) ? $_POST['pid'] : 0, 'f_name'=>isset($_POST['fname']) ? $_POST['fname'] : NULL, 'f_url'=>isset($_POST['furl']) ? $_POST['furl'] : NULL, 'f_desc'=>isset($_POST['fdesc']) ? trim($_POST['fdesc']) : NULL, 'f_order'=>isset($_POST['forder']) ? intval($_POST['forder']) : 0, 'f_level'=>$_POST['pid'] == 0 ? 1 : 2, );
		if (! empty($data['f_name'])) {
			$lastid = $this->Tmenu->save($data);
			if ($lastid) {
				$this->flash('添加成功!', '/tmenu/edit/'.$lastid);
				return;
			}
			$this->flash('添加失败!', '/tmenu/newsub/'.$data['f_pid']);
			return;
		}
		$this->flash('添加失败!', '/tmenu/newsub/'.$data['f_pid']);
		return;

	}
	/**
     *  edit the menu info action
     *
     */
	function doedit() {
		$data = Array('id'=>isset($_POST['id']) ? $_POST['id'] : 0, 'f_name'=>isset($_POST['fname']) ? $_POST['fname'] : NULL, 'f_url'=>isset($_POST['furl']) ? $_POST['furl'] : NULL, 'f_desc'=>isset($_POST['fdesc']) ? trim($_POST['fdesc']) : NULL, 'f_order'=>isset($_POST['forder']) ? intval($_POST['forder']) : 0);
		if (0 != $data['id'] && ! empty($data['f_name'])) {

			if ($this->Tmenu->save($data)) {
				$this->flash('修改成功!', '/tmenu/edit/'.$data['id']);
				return;
			}
			$this->flash('修改失败!', '/tmenu/edit/'.$data['id']);
			return;
		}
		$this->flash('修改失败!', '/tmenu/edit/'.$data['id']);
		return;

	}

}
?>
